package org.budo.dingtalk.util;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.net.util.Base64;
import org.budo.support.java.net.util.UrlEncodeUtil;

/**
 * @author lmw
 */
public class DingtalkUtil {
    public static String sign(Long timestamp, String secretKey) {
        try {
            String stringToSign = timestamp + "\n" + secretKey;
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(secretKey.getBytes("UTF-8"), "HmacSHA256"));
            byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
            String sign = UrlEncodeUtil.encode(new String(Base64.encodeBase64(signData)), "UTF-8");
            return sign;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}